package com.lovo.config;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.lovo.token.SysToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;



@Component
public class MyInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String path=  request.getServletPath();
        System.out.println(path);
        Algorithm algorithm = Algorithm.HMAC256("68a34f98-7fd7-4fb7-956a-def1e491b641");
        String info="";
        JWTVerifier verifier = JWT.require(algorithm).build();
        String  token=request.getHeader("token");
        DecodedJWT jwt = verifier.verify(token); //认证token
        info=jwt.getClaim("info").asString();//
        info=SysToken.verifierJwt(token);
//        if(SysToken.verifierJwt(token).equals("502")){
//            return false;
//        }
        if(info.contains(path)){
            return true;
        }
     return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
